package com.baidumap.administrator.happystudy.ui;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ab.http.OnMessageResponse;
import com.ab.util.ToastUtil;
import com.baidumap.administrator.happystudy.popupwindow.PopupWindowData;
import com.baidumap.administrator.happystudy.R;
import com.baidumap.administrator.happystudy.api.Api;
import com.baidumap.administrator.happystudy.base.AppBaseActivity;
import com.baidumap.administrator.happystudy.base.Constant;
import com.baidumap.administrator.happystudy.base.widget.Tag;
import com.baidumap.administrator.happystudy.base.widget.TagListView;
import com.baidumap.administrator.happystudy.base.widget.TagView;
import com.baidumap.administrator.happystudy.model.CourseList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.OnClick;

public class CurriculumSchedule1Activity extends AppBaseActivity implements OnMessageResponse {


    @Bind(R.id.goback)
    LinearLayout goback;
    @Bind(R.id.week1)
    RelativeLayout week1;
    @Bind(R.id.week2)
    RelativeLayout week2;
    @Bind(R.id.week3)
    RelativeLayout week3;
    @Bind(R.id.week4)
    RelativeLayout week4;
    @Bind(R.id.datatime)
    ImageView datatime;
    @Bind(R.id.imageView21)
    ImageView imageView21;

    private TagListView mTagListView[] = new TagListView[5];
    private final List<Tag> mTags1 = new ArrayList<Tag>();
    private final List<Tag> mTags2 = new ArrayList<Tag>();
    private final List<Tag> mTags3 = new ArrayList<Tag>();
    private final List<Tag> mTags4 = new ArrayList<Tag>();
    private final List<Tag> mTags0 = new ArrayList<Tag>();
    private final String[] titles = {"毛泽东思想概论", "PS基础课", "大学英语", "色彩构成"};
    @Bind(R.id.layout_ke_cheng_empty)
    RelativeLayout layout_ke_cheng_empty;
    @Bind(R.id.layout_ke_cheng)
    LinearLayout layout_ke_cheng;

    @Override
    public int bindLayout() {
        return R.layout.activity_curricuum_schedule1;
    }

    @Override
    public void initView(View view) {
        super.initView(view);
        IniTagListView(view);
        Api.courseLists(this, sHelper.getString(Constant.STU_TOKEN), sHelper.getString(Constant.STU_UID));
    }

    @Override
    public void doBusiness(Context mContext) {
        super.doBusiness(mContext);
        setHeaderBack();
        initTitleRightLayout();
        setHeaderTitle(R.string.ke_cheng_biao);
    }

    public TagView tagView1;
    public Tag tag1;

    private void IniTagListView(View view) {
        mTagListView[0] = (TagListView) view.findViewById(R.id.tagview);
        mTagListView[1] = (TagListView) view.findViewById(R.id.tagview2);
        mTagListView[2] = (TagListView) view.findViewById(R.id.tagview3);
        mTagListView[3] = (TagListView) view.findViewById(R.id.tagview4);
        mTagListView[4] = (TagListView) view.findViewById(R.id.tagview5);
        //SetTag();
    }

    private void SetTag() {
        setUpData(mTags0);
        mTagListView[0].setTags(mTags0);
        mTagListView[0].setOnTagLongClickListener(new TagListView.OnTagLongClickListener() {
            @Override
            public void onTagLongClick(TagView tagView, Tag tag) {
                tagView1 = tagView;
                tag1 = tag;
                setBackground(tagView, tag);

            }
        });
        setUpData(mTags1);
        mTagListView[1].setTags(mTags1);
        mTagListView[1].setOnTagLongClickListener(new TagListView.OnTagLongClickListener() {
            @Override
            public void onTagLongClick(TagView tagView, Tag tag) {
                tagView1 = tagView;
                tag1 = tag;
                setBackground(tagView, tag);

            }
        });
        setUpData(mTags2);
        mTagListView[2].setTags(mTags2);
        mTagListView[2].setOnTagLongClickListener(new TagListView.OnTagLongClickListener() {
            @Override
            public void onTagLongClick(TagView tagView, Tag tag) {
                tagView1 = tagView;
                tag1 = tag;
                setBackground(tagView, tag);

            }
        });
        setUpData(mTags3);
        mTagListView[3].setTags(mTags3);
        mTagListView[3].setOnTagLongClickListener(new TagListView.OnTagLongClickListener() {
            @Override
            public void onTagLongClick(TagView tagView, Tag tag) {
                tagView1 = tagView;
                tag1 = tag;
                setBackground(tagView, tag);

            }
        });
        setUpData(mTags4);
        mTagListView[4].setTags(mTags4);
        mTagListView[4].setOnTagLongClickListener(new TagListView.OnTagLongClickListener() {
            @Override
            public void onTagLongClick(TagView tagView, Tag tag) {
                tagView1 = tagView;
                tag1 = tag;
                setBackground(tagView, tag);

            }
        });
    }

    private void setBackground(TagView tagView, Tag tag) {

        if (false == tag.isChecked()) {
            showNormalDialog();
        } else {
            PopupWindowData mPop = new PopupWindowData();
            mPop.initAddPicturePopuptWindow(CurriculumSchedule1Activity.this, tagView);
            tagView.setBackgroundResource(R.mipmap.kecheng);
            tag.setChecked(false);
        }

    }

    private void showNormalDialog() {
        /* @setIcon 设置对话框图标
         * @setTitle 设置对话框标题
         * @setMessage 设置对话框消息提示
         * setXXX方法返回Dialog对象，因此可以链式设置属性
         */
        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(CurriculumSchedule1Activity.this);
        normalDialog.setMessage("您确认要取消闹钟吗?");
        normalDialog.setPositiveButton("确定",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        tagView1.setBackgroundResource(R.mipmap.kecheng1);
                        tag1.setChecked(true);
                    }
                });
        normalDialog.setNegativeButton("关闭",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //...To-do
                    }
                });
        // 显示
        normalDialog.show();
    }

    private void setUpData(List<Tag> mTags) {
        for (int i = 0; i < 4; i++) {
            Tag tag = new Tag();
            tag.setId(i);
            tag.setChecked(true);
            tag.setTitle(titles[i]);
            mTags.add(tag);

        }
    }

    private void initTitleRightLayout() {
        mAbTitleBar.clearRightView();
        View rightViewMore = mInflater.inflate(R.layout.stu_add, null);
        mAbTitleBar.addRightView(rightViewMore);
        TextView iv_add = (TextView) rightViewMore.findViewById(R.id.iv_add);

        iv_add.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                ToastUtil.showMessage(CurriculumSchedule1Activity.this, "添加界面正在设计中...");
//                startActivity(NewQuestionActivity.class);
            }
        });


    }

    @OnClick({R.id.goback, R.id.week1, R.id.week2, R.id.week3, R.id.week4, R.id.week5, R.id.datatime})
    public void onClick(View view) {
        Bundle bundle = new Bundle();
        switch (view.getId()) {
            case R.id.goback:
                finish();
                break;
            case R.id.week1:

                bundle.putString("title", "星期一");
                startBundleActivity(bundle, CurriculumScheduleDetailedActivity.class);
                break;
            case R.id.week2:

                bundle.putString("title", "星期二");
                startBundleActivity(bundle, CurriculumScheduleDetailedActivity.class);
                break;
            case R.id.week3:
                bundle.putString("title", "星期三");
                startBundleActivity(bundle, CurriculumScheduleDetailedActivity.class);
                break;
            case R.id.week4:
                bundle.putString("title", "星期四");
                startBundleActivity(bundle, CurriculumScheduleDetailedActivity.class);
                break;
            case R.id.week5:
                bundle.putString("title", "星期五");
                startBundleActivity(bundle, CurriculumScheduleDetailedActivity.class);
                break;
            case R.id.datatime:
                PopupWindowData mPop = new PopupWindowData();
                mPop.initAddPicturePopuptWindow(CurriculumSchedule1Activity.this, view);
                break;
        }
    }

    @Override
    public void onMessageResponse(String url, JSONObject jo) throws JSONException {
        if (url.equals(Constant.COURSE_LISTS) && jo != null) {
            CourseList courseList = new CourseList(jo.toString());

            if (courseList != null) {


                if (courseList.getCode().equals("200")) {
                    layout_ke_cheng.setVisibility(View.VISIBLE);
                    layout_ke_cheng_empty.setVisibility(View.GONE);
                    List<CourseList.DataBean.MondayBean> mondayBeanList = courseList.getData().getMonday();
                    for (int i = 0; i < mondayBeanList.size(); i++) {
                        Tag tag = new Tag();
                        tag.setId(i);
                        tag.setChecked(true);
                        tag.setTitle(mondayBeanList.get(i).getCourse());
                        mTags0.add(tag);

                    }
                    mTagListView[0].setTags(mTags0);


                    List<CourseList.DataBean.TuesdayBean> tuesdayBeanList = courseList.getData().getTuesday();
                    for (int i = 0; i < tuesdayBeanList.size(); i++) {
                        Tag tag = new Tag();
                        tag.setId(i);
                        tag.setChecked(true);
                        tag.setTitle(tuesdayBeanList.get(i).getCourse());
                        mTags1.add(tag);

                    }
                    mTagListView[1].setTags(mTags1);

                    List<CourseList.DataBean.ThursdayBean> thursdayBeanList = courseList.getData().getThursday();
                    for (int i = 0; i < thursdayBeanList.size(); i++) {
                        Tag tag = new Tag();
                        tag.setId(i);
                        tag.setChecked(true);
                        tag.setTitle(thursdayBeanList.get(i).getCourse());
                        mTags2.add(tag);

                    }
                    mTagListView[2].setTags(mTags2);

                    List<CourseList.DataBean.WensedayBean> wensedayBeanList = courseList.getData().getWenseday();
                    for (int i = 0; i < wensedayBeanList.size(); i++) {
                        Tag tag = new Tag();
                        tag.setId(i);
                        tag.setChecked(true);
                        tag.setTitle(wensedayBeanList.get(i).getCourse());
                        mTags3.add(tag);

                    }
                    mTagListView[3].setTags(mTags3);

                    List<CourseList.DataBean.FridayBean> fridayBeanList = courseList.getData().getFriday();
                    for (int i = 0; i < fridayBeanList.size(); i++) {
                        Tag tag = new Tag();
                        tag.setId(i);
                        tag.setChecked(true);
                        tag.setTitle(fridayBeanList.get(i).getCourse());
                        mTags3.add(tag);

                    }
                    mTagListView[4].setTags(mTags4);


                } else {
                    ToastUtil.showMessage(this, courseList.getMsg());
                    layout_ke_cheng.setVisibility(View.GONE);
                    layout_ke_cheng_empty.setVisibility(View.VISIBLE);

                }
            }

        }
    }

    @Override
    public void onMessageResponse(String url, JSONArray jo) throws JSONException {

    }

    @Override
    public void onMessageResponse(String url, String str) throws Exception {

    }
}
